package com.sukumar.android.sms.activity;

import java.util.regex.Pattern;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPrefPassword;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast;

import com.sukumar.android.sms.R;
import com.sukumar.android.sms.service.SmsManagerService;

public class SMSPreferenceActivity extends PreferenceActivity {
	ListPreference emailprovider;
	EditTextPreference emailAddress;
	EditTextPrefPassword emailPassword;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.sms_preference);

		final Pattern EMAIL_ADDRESS_PATTERN = Pattern
				.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@"
						+ "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\."
						+ "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + ")+");

		CheckBoxPreference receiverEnbled = (CheckBoxPreference) findPreference(getText(R.string.enable_receiver_key));
		CheckBoxPreference sendBgEmail = (CheckBoxPreference) findPreference(getText(R.string.direct_email_key));

		emailprovider = (ListPreference) findPreference(getText(R.string.email_provider_key));
		emailAddress = (EditTextPreference) findPreference(getText(R.string.email_address_key));
		emailPassword = (EditTextPrefPassword) findPreference(getText(R.string.email_password_key));

		Preference usagePref = (Preference) findPreference(getText(R.string.Usage_btn));
		Preference changeLogPref = (Preference) findPreference(getText(R.string.Changelog_btn));
		Preference aboutPref = (Preference) findPreference(getText(R.string.About));

		setDefaultValue();

		receiverEnbled
				.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
					@Override
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {
						Boolean newBoolValue = (Boolean) newValue;
						if (newBoolValue == true) {
							startService(new Intent(getApplicationContext(),
									SmsManagerService.class));
						} else {
							stopService(new Intent(getApplicationContext(),
									SmsManagerService.class));
						}
						return true;

					}
				});

		sendBgEmail
				.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
					@Override
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {
						Boolean newBoolValue = (Boolean) newValue;
						if (newBoolValue == true) {
							emailprovider.setEnabled(true);
							emailAddress.setEnabled(true);
							emailPassword.setEnabled(true);
						} else {
							Editor prefEditor = PreferenceManager
									.getDefaultSharedPreferences(
											getApplicationContext()).edit();
							prefEditor.remove(getText(
									R.string.email_provider_key).toString());
							prefEditor.remove(getText(
									R.string.email_address_key).toString());
							prefEditor.remove(getText(
									R.string.email_password_key).toString());
							emailprovider.setEnabled(false);
							emailAddress.setEnabled(false);
							emailPassword.setEnabled(false);
							prefEditor.commit();
						}
						return true;

					}
				});

		emailAddress
				.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

					@Override
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {
						boolean validEmail = EMAIL_ADDRESS_PATTERN.matcher(
								newValue.toString()).matches();
						if (validEmail) {
							return true;
						} else {
							Toast.makeText(
									getApplicationContext(),
									"Invalid email address: "
											+ newValue.toString(),
									Toast.LENGTH_SHORT).show();
							return false;
						}
					}
				});

		usagePref
				.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference paramPreference) {
						Intent myIntent = new Intent(getApplicationContext(),
								SMSAboutActivity.class);
						myIntent.putExtra("selected", paramPreference.getKey());
						startActivity(myIntent);
						return true;
					}
				});

		changeLogPref
				.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference paramPreference) {
						Intent myIntent = new Intent(getApplicationContext(),
								SMSAboutActivity.class);
						myIntent.putExtra("selected", paramPreference.getKey());
						startActivity(myIntent);
						return true;
					}
				});

		aboutPref
				.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference paramPreference) {
						Intent myIntent = new Intent(getApplicationContext(),
								SMSAboutActivity.class);
						myIntent.putExtra("selected", paramPreference.getKey());
						startActivity(myIntent);
						return true;
					}
				});

	}

	private void setDefaultValue() {
		CheckBoxPreference receiverEnbled = (CheckBoxPreference) findPreference(getText(R.string.enable_receiver_key));
		CheckBoxPreference sendBgEmail = (CheckBoxPreference) findPreference(getText(R.string.direct_email_key));

		receiverEnbled.setChecked(false);
		ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager
				.getRunningServices(Integer.MAX_VALUE)) {
			if ("com.sukumar.android.sms.SmsManagerService"
					.equals(service.service.getClassName())) {
				receiverEnbled.setChecked(true);
			}
		}
		if (sendBgEmail.isChecked()) {
			emailprovider.setEnabled(true);
			emailAddress.setEnabled(true);
			emailPassword.setEnabled(true);
		} else {
			emailprovider.setEnabled(false);
			emailAddress.setEnabled(false);
			emailPassword.setEnabled(false);
		}

	}
}
